using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Authorization.Permissions;
using Joy.Erp.BasicArchives.Localization;

namespace Joy.Erp.BasicArchives.Permissions
{
    public class ReceivementPermissionDefinitionPovider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(BasicArchivesPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(BasicArchivesPermissions.Menus, L("Permission:Menus"));
            }

            var purchasingManagementMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.BasicArchivesMenu.PurchasingManagement);
            if (purchasingManagementMenu == null)
            {
                purchasingManagementMenu = menus.AddPermission(BasicArchivesPermissions.BasicArchivesMenu.PurchasingManagement, L("Permission:Menus.BasicArchives.PurchasingManagement"));
            }

            var parentMenu = purchasingManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.Receivement.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = purchasingManagementMenu.AddChild(BasicArchivesPermissions.Receivement.ParentMenu, L("Permission:Menus.BasicArchives.PurchasingManagement.Receipt"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.Receivement.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(BasicArchivesPermissions.Receivement.Menu, L("Permission:Menus.BasicArchives.PurchasingManagement.Receipt.Receivement"));
            }

            var group = context.GetGroupOrNull(BasicArchivesPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(BasicArchivesPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pReceivementDefault = group.AddPermission(BasicArchivesPermissions.Receivement.Default, L("Permission:BasicArchives.Receipt.Receivement"));
            var pReceivementQuery = pReceivementDefault.AddChild(BasicArchivesPermissions.Receivement.Query, L("Permission:Query"));
            var pReceivementEdit = pReceivementDefault.AddChild(BasicArchivesPermissions.Receivement.Edit, L("Permission:Edit"));
            var pCreate = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.Create, L("Permission:Create"));
            var pUpdate = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.Update, L("Permission:Update"));
            var pDelete = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.Delete, L("Permission:Delete"));
            var pSubmit = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.Submit, L("Permission:Submit"));
            var pApprove = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.Approve, L("Permission:Approve"));
            var pUnApprove = pReceivementEdit.AddChild(BasicArchivesPermissions.Receivement.UnApprove, L("Permission:UnApprove"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
} 